package com.insight.config;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.ilm.ElasticsearchIlmClient;
import co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ElasticSearch 客户端配置类，提供同步、异步、索引、生命周期等客户端Bean。
 */
@Configuration
@EnableConfigurationProperties(ElasticsearchRestClientProperties.class)
public class ElasticSearchConfig {

    /**
     * 注入ElasticsearchTransport Bean
     * @param restClient RestClient实例
     * @return ElasticsearchTransport实例
     */
    @Bean
    public ElasticsearchTransport transport(RestClient restClient) {
        return new RestClientTransport(restClient, new JacksonJsonpMapper());
    }

    /**
     * 注入Elasticsearch同步客户端Bean
     * @param transport 传输层
     * @return ElasticsearchClient实例
     */
    @Bean
    public ElasticsearchClient elasticsearchClient(ElasticsearchTransport transport) {
        return new ElasticsearchClient(transport);
    }

    /**
     * 注入Elasticsearch异步客户端Bean
     * @param transport 传输层
     * @return ElasticsearchAsyncClient实例
     */
    @Bean
    public ElasticsearchAsyncClient elasticsearchAsyncClient(ElasticsearchTransport transport) {
        return new ElasticsearchAsyncClient(transport);
    }

    /**
     * 注入Elasticsearch索引客户端Bean
     * @param transport 传输层
     * @return ElasticsearchIndicesClient实例
     */
    @Bean
    public ElasticsearchIndicesClient elasticsearchIndicesClient(ElasticsearchTransport transport) {
        return new ElasticsearchIndicesClient(transport);
    }

    /**
     * 注入Elasticsearch生命周期管理客户端Bean
     * @param transport 传输层
     * @return ElasticsearchIlmClient实例
     */
    @Bean
    public ElasticsearchIlmClient elasticsearchIlmClient(ElasticsearchTransport transport) {
        return new ElasticsearchIlmClient(transport);
    }

}
